﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OfficeToSwf;

namespace XMK.Tadong.OfficeToSwf
{
    public enum BKExecItemState { 待执行, 完成, 出错 };
    /// <summary>
    /// 文件
    /// </summary>
    public class ExecItem
    {
        #region 基本属性
        private string ErrMsg = "";
        /// <summary>
        /// 文件ID
        /// </summary>
        private int m_FileID;
        public int FileID
        {
            get { return m_FileID; }
            set { m_FileID = value; }
        }

        /// <summary>
        /// 文件根目录
        /// </summary>
        private string m_UpLoadPath;
        public string UpLoadPath
        {
            get { return m_UpLoadPath; }
            set { m_UpLoadPath = value; }
        }

        /// <summary>
        /// office文件地址
        /// </summary>
        private string m_OfficeFilePath;
        public string OfficeFilePath
        {
            get { return m_OfficeFilePath; }
            set { m_OfficeFilePath = value; }
        }
        /// <summary>
        /// pdf文件地址
        /// </summary>
        private string m_PDFFilePath;
        public string PDFFilePath
        {
            get { return m_PDFFilePath; }
            set { m_PDFFilePath = value; }
        }
        /// <summary>
        /// SWF文件地址
        /// </summary>
        private string m_SWFFilePath;
        public string SWFFilePath
        {
            get { return m_SWFFilePath; }
            set { m_SWFFilePath = value; }
        }
        /// <summary>
        /// 源文件后缀 大写
        /// </summary>
        private string m_SourceFileSuffix;
        public string SourceFileSuffix
        {
            get { return m_SourceFileSuffix; }
            set { m_SourceFileSuffix = value; }
        }
        /// <summary>
        /// PDF文件执行状态 1：执行中，0：失败
        /// </summary>
        private BKExecItemState m_PDFExecState;
        public BKExecItemState PDFExecState
        {
            get { return m_PDFExecState; }
            set { m_PDFExecState = value; }
        }

        /// <summary>
        /// PDF文件执行状态 1：执行中，0：失败
        /// </summary>
        private BKExecItemState m_SWFExecState;
        public BKExecItemState SWFExecState
        {
            get { return m_SWFExecState; }
            set { m_SWFExecState = value; }
        }

        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="fileID">文件ID</param>
        /// <param name="officeFilePath">文件路径</param>
        public ExecItem(int fileID, string officeFilePath)
        {
            #region

            m_FileID = fileID;
            m_OfficeFilePath = officeFilePath;
            m_PDFExecState = BKExecItemState.待执行;
            m_SWFExecState = BKExecItemState.待执行;
            #endregion
        }

        /// <summary>
        /// 执行转换任务
        /// </summary>
        /// <returns></returns>
        public bool ExecFileConvert()
        {
            #region
            //先判断是不是 pdf jpg等 如果不是再用
            if (m_SourceFileSuffix == "PDF")
            {
                if (ExecPDFToSWF())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            //else if (m_SourceFileSuffix == "JPG" || m_SourceFileSuffix == "PNG" || m_SourceFileSuffix == "GIF")
            //{
            ////转换可以成功，但在浏览器中打不开
            //    if (ExecPICToSWF())
            //    {
            //        return true;
            //    }
            //    else
            //    {
            //        return false;
            //    }
            //}
            else
            {
                if (ExecOfficeToPDF())
                {
                    if (ExecPDFToSWF())
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }

            #endregion
        }

        /// <summary>
        /// 执行office文件转换为PDF文件
        /// </summary>
        /// <returns></returns>
        private bool ExecOfficeToPDF()
        {
            #region
            try
            {
                DateTime dtStart = DateTime.Now;
                bool bOK = Office2Pdf.ConvertToPDF(m_OfficeFilePath, m_PDFFilePath);
                DateTime dtEnd = DateTime.Now;
                
                TimeSpan ts = dtEnd - dtStart;
                string strTime = "转换用时： " + ts.Hours.ToString() + " 小时 " + ts.Minutes.ToString() + " 分 " + ts.Seconds.ToString() + " 秒";
                
                if (bOK)
                {
                    System.Threading.Thread.Sleep(BkExecManager.PDFDueTime);
                    return true;
                }
                else
                {
                    System.Threading.Thread.Sleep(BkExecManager.PDFDueTime);
                    return false;
                }

                
            }
            catch (Exception ex)
            {
                System.Threading.Thread.Sleep(BkExecManager.PDFDueTime);
                return false;
            }

            #endregion
        }

        /// <summary>
        /// 执行PDF文件转换为SWF文件
        /// </summary>
        /// <param name="isPoly2Bitmap">是否是采用把图片转换成bitmap的形式</param>
        /// <returns></returns>
        private bool ExecPDFToSWF(bool isPoly2Bitmap = false)
        {
            #region
            try
            {
                DateTime dtStart = DateTime.Now;
                Pdf2Swf.PDFConvertToSWF(m_PDFFilePath, m_SWFFilePath, isPoly2Bitmap);
                //等待
                System.Threading.Thread.Sleep(BkExecManager.SWFDueTime);
                DateTime dtEnd = DateTime.Now;
                int i=0;
                
                while (!System.IO.File.Exists(m_SWFFilePath) && i < 5)
                {
                    ++i;
                    System.Threading.Thread.Sleep(BkExecManager.SWFDueTime);
                    dtEnd = DateTime.Now;
                }
                if (System.IO.File.Exists(m_SWFFilePath))
                {
                    TimeSpan ts = dtEnd - dtStart;
                    string strTime = "转换用时： " + ts.Hours.ToString() + " 小时 " + ts.Minutes.ToString() + " 分 " + ts.Seconds.ToString() + " 秒";
                    return true;
                }
                else
                {

                    #region 采用其他参数重新转换 转换失败有可能是因为shape过多造成的
                    if (!isPoly2Bitmap)
                    {
                        ExecPDFToSWF(true);
                    }
                    #endregion

                    return false;
                }
                
            }
            catch (Exception ex)
            {
                return false;
            }

            #endregion
        }
        /// <summary>
        /// 执行PIC文件转换为SWF文件
        /// </summary>
        /// <returns></returns>
        private bool ExecPICToSWF()
        {
            #region
            try
            {
                DateTime dtStart = DateTime.Now;
                Pdf2Swf.PICConvertToSWF(m_PDFFilePath, m_SWFFilePath);
                //等待
                System.Threading.Thread.Sleep(BkExecManager.SWFDueTime);
                DateTime dtEnd = DateTime.Now;

                if (System.IO.File.Exists(m_SWFFilePath))
                {
                    TimeSpan ts = dtEnd - dtStart;
                    string strTime = "转换用时： " + ts.Hours.ToString() + " 小时 " + ts.Minutes.ToString() + " 分 " + ts.Seconds.ToString() + " 秒";
                    
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                return false;
            }

            #endregion
        }


    }
}

